package com.decent.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.decent.dao.UserMapper;
import com.decent.entity.User;
import com.decent.entity.common.Message;
import com.decent.service.UserService;
import com.decent.util.MessageEnum;
import com.decent.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author decent
 * @Create 2021/4/1 - 11:14
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录验证
     *
     * @param user 账号密码
     * @return 存在用户返回1
     */
    @Override
    public Message login(User user) {
        if (user.getUpassword() == null) {
            User selectOne = userMapper.selectOne(Wrappers.<User>query().eq("uname", user.getUname()));
            if (Objects.nonNull(selectOne)) {
                return MessageUtils.buildMsg(MessageEnum.LOGIN_PASSWORD_ERROR);
            }
        } else {
            User selectOne = userMapper.selectOne(Wrappers.<User>query()
                    .eq("uname", user.getUname())
                    .eq("upassword", user.getUpassword()));
            if (Objects.nonNull(selectOne)) {
                return MessageUtils.buildMsg(MessageEnum.LOGIN_SUCCESS);
            }
        }
        return MessageUtils.buildMsg(MessageEnum.LOGIN_ERROR);
    }

}
